<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="查询类型" prop="accType">
        <el-select
          v-model="queryParams.accType"
          placeholder="请选择查询类型"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.wtype"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键字" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        :label="selectDictLabel(dict.type.wtype, queryParams.accType) + '名称'"
        align="center"
        prop="username"
      />
      <el-table-column label="类型" align="center" prop="type" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bill_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column
        label="变更前余额"
        align="center"
        prop="balance"
        width="100"
      />
      <el-table-column
        label="变更后余额"
        align="center"
        prop="newBalance"
        width="100"
      />
      <el-table-column label="变更金额" align="center" width="100">
        <template slot-scope="{ row }">
          <span :style="row.pm == 1 ? 'color:#67C23A;' : 'color:#F56C6C;'"
            >{{ row.pm == 1 ? "+" : "-" }}{{ row.number }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        min-width="200"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改余额日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="80px">
        <el-form-item label="${comment}" prop="playNo">
          <el-input v-model="form.playNo" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="用户id" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="名字" prop="username">
          <el-input v-model="form.username" placeholder="请输入名字" />
        </el-form-item>
        <el-form-item label="头像" prop="headimg">
          <el-input v-model="form.headimg" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="支出收入" prop="pm">
          <el-input v-model="form.pm" placeholder="请输入支出收入" />
        </el-form-item>
        <el-form-item label="操作后余额" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入操作后余额" />
        </el-form-item>
        <el-form-item label="变更金额" prop="number">
          <el-input v-model="form.number" placeholder="请输入变更金额" />
        </el-form-item>
        <el-form-item label="专家编号" prop="expId">
          <el-input v-model="form.expId" placeholder="请输入专家编号" />
        </el-form-item>
        <el-form-item label="单位编号" prop="comId">
          <el-input v-model="form.comId" placeholder="请输入单位编号" />
        </el-form-item>
        <el-form-item label="渠道编号" prop="channelId">
          <el-input v-model="form.channelId" placeholder="请输入渠道编号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
} from "@/api/order/balancelog";
import { selectDictLabel } from "@/utils/ruoyi.js";
export default {
  name: "Balancelog",
  dicts: ["wtype", "bill_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 余额日志表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        playNo: null,
        uid: null,
        username: null,
        headimg: null,
        accType: "1",
        orderId: null,
        pm: null,
        type: null,
        balance: null,
        number: null,
        expId: null,
        comId: null,
        channelId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    selectDictLabel,
    /** 查询余额日志列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        playNo: null,
        uid: null,
        username: null,
        headimg: null,
        accType: null,
        orderId: null,
        pm: null,
        type: null,
        balance: null,
        number: null,
        createTime: null,
        updateTime: null,
        expId: null,
        comId: null,
        channelId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加余额日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改余额日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除余额日志编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  },
};
</script>
